package LUDOSimulator;

public class DbjLUDOPlayer implements LUDOPlayer
{
	LUDOBoard board;
	private int[] weights;
	private int weight1, weight2, weight3, weight4, weight5, weight6, weight7, weight8, weight9, weight10;
	
	public DbjLUDOPlayer(LUDOBoard board)
	{
		this.board = board;
		weight1 = 78;
		weight2 = 71;
		weight3 = 50;
		weight4 = 77;
		weight5 = 38;
		weight6 = 6;
		weight7 = 40;
		weight8 = 20;
		weight9 = 0;
		weight10 = 58;
	}
	
	public DbjLUDOPlayer(LUDOBoard board, int[] w)
	{
		this.board = board;
		this.weight1 = w[0];
		this.weight2 = w[1];
		this.weight3 = w[2];
		this.weight4 = w[3];
		this.weight5 = w[4];
		this.weight6 = w[5];
		this.weight7 = w[6];
		this.weight8 = w[7];
		this.weight9 = w[8];
		this.weight10 = w[9];
		this.weights = w;
	}

	public DbjLUDOPlayer(LUDOBoard board, int w1, int w2, int w3, int w4, int w5, int w6, int w7, int w8, int w9, int w10)
	{
		this.board = board;
		this.weight1 = w1;
		this.weight2 = w2;
		this.weight3 = w3;
		this.weight4 = w4;
		this.weight5 = w5;
		this.weight6 = w6;
		this.weight7 = w7;
		this.weight8 = w8;
		this.weight9 = w9;
		this.weight10 = w10;
	}
	
	public int[] getWeights()
	{
		return this.weights;
	}
	
	public void setWeights(int[] newWeights)
	{
		this.weights = newWeights;
	}
	
	public void play()
	{
		board.print("Dbj player playing");

		board.rollDice();
		int max =-1;
		int bestIndex = -1;
		for (int i = 0; i < 4; i++)
		{
			int value = analyzeBrickSituation(i); 
			if (value > max && value > 0)
			{
				bestIndex = i;
				max = value;
			}
		}
		if (bestIndex != -1)
			board.moveBrick(bestIndex);
	}
	
	public int analyzeBrickSituation(int i)
	{
		if(board.moveable(i))
		{
			int[][] current_board = board.getBoardState();
			int[][] new_board = board.getNewBoardState(i, board.getMyColor(), board.getDice());
			
			boolean hitMySelfHome = hitMySelfHome(current_board, new_board, i);
			
			int weightToReturn = 1;
			boolean actionFound = false;
			if (hitOpponentHome(current_board, new_board))
			{
				if (this.weight1 > weightToReturn)
					weightToReturn = this.weight1;
				actionFound = true;
			}
			if (board.isStar(new_board[board.getMyColor()][i]) && !hitMySelfHome)
			{
				if (this.weight2 > weightToReturn)
					weightToReturn = this.weight2;
				actionFound = true;
			}
			if (moveOut(current_board, new_board, i) && !hitMySelfHome)
			{
				if (this.weight3 > weightToReturn)
					weightToReturn = this.weight3;
				actionFound = true;
			}
			if (board.atHome(new_board[board.getMyColor()][i], board.getMyColor()))
			{
				if (this.weight4 > weightToReturn)
					weightToReturn = this.weight4;
				actionFound = true;
			}
			//if (jointATeamMemberOnLocation(board.getMyBricks(), board.getDice(), i))
			if (jointATeamMemberOnLocation(current_board, new_board, i))
			{
				if (this.weight5 > weightToReturn)
					weightToReturn = this.weight5;
				actionFound = true;
			}
			if (isFrontBrick(current_board, i))
			{
				if (this.weight6 > weightToReturn)
					weightToReturn = this.weight6;
				actionFound = true;
			}
			if (isLastBrick(current_board, i))
			{
				if (this.weight7 > weightToReturn)
					weightToReturn = this.weight7;
				actionFound = true;
			}
			if (board.isGlobe(new_board[board.getMyColor()][i]) && !hitMySelfHome)
			{
				if (this.weight8 > weightToReturn)
					weightToReturn = this.weight8;
				actionFound = true;
			}
			if (board.almostHome(new_board[board.getMyColor()][i], board.getMyColor()))
			{
				if (this.weight9 > weightToReturn)
					weightToReturn = this.weight9;
				actionFound = true;
			}
			if (standingJustInFrontOfOpponent(new_board, i) && !hitMySelfHome)
			{
				if (this.weight10 > weightToReturn)
					weightToReturn = this.weight10;
				actionFound = true;
			}
			if (!actionFound)
				return 1;
			return weightToReturn;
		}
		else
			return 0;
	}

	private boolean hitOpponentHome(int[][] current_board, int[][] new_board)
	{
		for(int i = 0; i < 4; i++)
		{
			for(int j = 0; j < 4; j++)
			{
				if (board.getMyColor() != i)
					if(board.atField(current_board[i][j]) &&
					   !board.atField(new_board[i][j]))
						return true;
			}
		}
		return false;
	}
	
	private boolean jointATeamMemberOnLocation(int[][] current_board, int[][] new_board, int currentBrick)
	{
		if (!board.almostHome(new_board[board.getMyColor()][currentBrick], board.getMyColor()))
			for(int brick = 0; brick < 4; brick++)
				if (brick != currentBrick &&
					current_board[board.getMyColor()][brick] == new_board[board.getMyColor()][currentBrick])
					return true;
		return false;
	}
	
	private boolean standingJustInFrontOfOpponent(int[][] new_board, int currentBrick)
	{
		for (int color = 0; color < 4; color++)
		{
			if (color != board.getMyColor())
			{
				for (int diceValue = 1; diceValue < 7; diceValue++)
				{
					for (int brickNo = 0; brickNo < 4; brickNo++)
					{
						int[][] possibleFutureBoard = board.getNewBoardState(brickNo, color, diceValue);
						if (hitMySelfHome(new_board, possibleFutureBoard, currentBrick))
							return true;
					}
				}
			}
		}
		return false;
	}
	
	private boolean hitMySelfHome(int[][] current_board, int[][] new_board, int currentBrick)
	{
		if (!board.inStartArea(current_board[board.getMyColor()][currentBrick], board.getMyColor()) &&
			board.inStartArea(new_board[board.getMyColor()][currentBrick], board.getMyColor())) 
			return true;
		return false;
	}
	
	private boolean moveOut(int[][] current_board, int[][] new_board, int currentBrick)
	{
		if (board.inStartArea(current_board[board.getMyColor()][currentBrick], board.getMyColor()) && 
		    !board.inStartArea(new_board[board.getMyColor()][currentBrick], board.getMyColor()))
			return true;
		return false;
	}
	
	private boolean isFrontBrick(int[][] current_board, int currentBrick)
	{
		int[] brickPositions = board.getMyBricks();
		
		if (board.inStartArea(current_board[board.getMyColor()][currentBrick], board.getMyColor()))
			return false;
		
		if (board.getMyColor() == board.YELLOW)
		{
			for (int i = 0; i < 4; i++)
			{
				if (i != currentBrick)
					if (!(board.inStartArea(current_board[board.getMyColor()][i], board.getMyColor())))
						if (brickPositions[i] >= brickPositions[currentBrick])
							return false;
			}
		}
		
		if (board.getMyColor() == board.RED)
		{
			for (int i = 0; i < 4; i++)
			{
				if (brickPositions[i] < 13)
					brickPositions[i] = brickPositions[i] + 52;
				if (i != currentBrick)
					if (!(board.inStartArea(current_board[board.getMyColor()][i], board.getMyColor())))
						if (brickPositions[i] >= brickPositions[currentBrick])
							return false;
			}
		}
		
		if (board.getMyColor() == board.BLUE)
		{
			for (int i = 0; i < 4; i++)
			{
				if (brickPositions[i] < 26)
					brickPositions[i] = brickPositions[i] + 52;
				if (i != currentBrick)
					if (!(board.inStartArea(current_board[board.getMyColor()][i], board.getMyColor())))
						if (brickPositions[i] >= brickPositions[currentBrick])
							return false;
			}
		}
		
		if (board.getMyColor() == board.GREEN)
		{
			for (int i = 0; i < 4; i++)
			{
				if (brickPositions[i] < 39)
					brickPositions[i] = brickPositions[i] + 52;
				if (i != currentBrick)
					if (!(board.inStartArea(current_board[board.getMyColor()][i], board.getMyColor())))
						if (brickPositions[i] >= brickPositions[currentBrick])
							return false;
			}
		}
		
		return true;
	}
	
	private boolean isLastBrick(int[][] current_board, int currentBrick)
	{
		int[] brickPositions = board.getMyBricks();
		
		if (board.inStartArea(current_board[board.getMyColor()][currentBrick], board.getMyColor()))
			return false;
		
		if (board.getMyColor() == board.YELLOW)
		{
			for (int i = 0; i < 4; i++)
			{
				if (i != currentBrick)
					if (!(board.inStartArea(current_board[board.getMyColor()][i], board.getMyColor())))
						if (brickPositions[i] <= brickPositions[currentBrick])
							return false;
			}
		}
		
		if (board.getMyColor() == board.RED)
		{
			for (int i = 0; i < 4; i++)
			{
				if (brickPositions[i] < 13)
					brickPositions[i] = brickPositions[i] + 52;
				if (i != currentBrick)
					if (!(board.inStartArea(current_board[board.getMyColor()][i], board.getMyColor())))
						if (brickPositions[i] <= brickPositions[currentBrick])
							return false;
			}
		}
		
		if (board.getMyColor() == board.BLUE)
		{
			for (int i = 0; i < 4; i++)
			{
				if (brickPositions[i] < 26)
					brickPositions[i] = brickPositions[i] + 52;
				if (i != currentBrick)
					if (!(board.inStartArea(current_board[board.getMyColor()][i], board.getMyColor())))
						if (brickPositions[i] <= brickPositions[currentBrick])
							return false;
			}
		}
		
		if (board.getMyColor() == board.GREEN)
		{
			for (int i = 0; i < 4; i++)
			{
				if (brickPositions[i] < 39)
					brickPositions[i] = brickPositions[i] + 52;
				if (i != currentBrick)
					if (!(board.inStartArea(current_board[board.getMyColor()][i], board.getMyColor())))
						if (brickPositions[i] <= brickPositions[currentBrick])
							return false;
			}
		}
		return true;
	}
}
